<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/6/5
 * Time: 16:19
 */

namespace app\mobile\model;


use think\Model;
use think\Db;

class TeacherHomeworkModel extends Model
{
    public function  getList($filter)
    {
        $where=[
            'p.shop_id'=> $filter['admin_shop_id'],
            'c.shop_id'=> $filter['admin_shop_id'],
            'u.shop_id'=> $filter['admin_shop_id'],

        ];
        $field='p.id id,p.update_time,p.create_time,p.class_id,p.lesson_theme,p.content,p.progress,u.user_login,u.id uid,c.name cname,c.teacher_id';
        //当前用户的id
        $current_id=cmf_get_current_admin_id();
        //判断是否老师登录或者非老师登录
        $role_id=Db::name('role_user')->alias('c')
            ->join('role r','r.id=c.role_id')
            ->where(['user_id'=>$current_id])
            ->value('r.name');



        if(!empty($filter['select_teacher_id']))
        {
            $where['u.id']=$filter['select_teacher_id'];
        }

        if(!empty($filter['class']))
        {
            $where['p.class_id']=$filter['class'];
        }

        $homework_starttime = empty($filter['homework_starttime']) ? 0 : strtotime($filter['homework_starttime']);
        $homework_endtime   = empty($filter['homework_endtime']) ? 0 : strtotime($filter['homework_endtime']);
        if (!empty($homework_starttime) && !empty($homework_endtime)) {
            $where['p.create_time'] = [['>= time', $homework_starttime], ['<= time', $homework_endtime]];
        } else {
            if (!empty($homework_starttime)) {
                $where['p.create_time'] = ['>= time', $homework_starttime];
            }
            if (!empty($homework_endtime)) {
                $where['p.create_time'] = ['<= time', $homework_endtime];
            }
        }
        if($role_id=='教师')
        {
            $w=[
                'c.delete_time' =>0,
                'p.delete_time'=>0,
                'p.teacher_id'=>$current_id,

            ];
            $all=array_merge($w,$where);
            $data=Db::name('teacher_homework')->alias('p')->field($field)
                ->join('class c','c.id=p.class_id')
                ->join('user u','u.id=p.teacher_id')
                ->where($all)
                ->where('c.class_status','<>','2')
                ->order('p.id','DESC')
                ->paginate(10,false,['query' => $filter]);

        }
        if($role_id=='超级管理员'|| $role_id=='教导主任' ||$role_id=='校长')
        {
            $w=[
                'c.delete_time' =>0,
                'p.delete_time'=>0,
            ];
            $all=array_merge($w,$where);
            $data=Db::name('teacher_homework')->alias('p')->field($field)
                ->join('class c','c.id=p.class_id')
                ->join('user u','u.id=p.teacher_id')
                ->where($all)
                ->where('c.class_status','<>','2')
                ->order('p.id','DESC')
                ->paginate(10,false,['query' => $filter]);
        }
        return $data;

    }
    //所有的班级
    public function class_all()
    {
        $current_id=cmf_get_current_admin_id();
        $data=[];
        //判断是否老师登录或者非老师登录
        //判断是否老师登录或者非老师登录
        $role_id=Db::name('role_user')->alias('c')
            ->join('role r','r.id=c.role_id')
            ->where(['user_id'=>$current_id])
            ->value('r.name');
        if($role_id=='教师')
        {
            $field='p.id id,p.name';
            $w=[
                'p.teacher_id'=>$current_id,

            ];
            $data=Db::name('class')->alias('p')->field($field)
                ->join('user u','u.id=p.teacher_id')
                ->where($w)
                ->where('p.class_status','<>','2')
                ->where(['delete_time'=>0])
                ->select()
                ->toArray();
        }else{
            $data=Db::name('class')->where('delete_time','0')->select()->toArray();
        }
        return $data;

    }
}